<?php
namespace Appstore\Widget;

use Think\Controller;

class PingxxWidget extends Controller
{
    public function getAmount($order){
        $finally =$order['payment_price'];
        $score_id=$order['score'];
        $fields_config = modC('RE_FIELD', "", 'Pingxx');
        $fields = json_decode($fields_config,true);
        $type = array_search_key($fields,'FIELD',$score_id);
        if( !$type){
            return 0;
        }
        return number_format($finally/$type['UNIT'], 2, ".", "");
    }



    public function getParams($order_id){

        $order =D('Appstore/AppstoreOrder')->getById($order_id);
        $channel = I('post.channel');
        $aSubject = I('post.subject',modC('WEB_SITE_NAME', 'OpenSNS开源社交系统', 'Config').'云市场在线支付');
        $aBody = I('post.body',         '通过'.get_pay_method($channel).'进行云市场商品在线支付');
        if($order['pay_ok']){
            $this->error('该订单已经支付');
        }

        if($order['uid'] != is_login()){
            $this->error('不是您的订单');
        }
        return array('channel'=>$channel,'body'=>$aBody,'subject'=>$aSubject,'amount'=>$this->getAmount($order));
    }


    public function afterPay($event){

        $charge = $event['data']['object'];
                if($charge['paid']){
            $order_id = $charge['order_no'];
            D('Appstore/AppstoreOrder')->afterPay($order_id);
        }

        return '';
    }

}